package com.zyw.customcalendar.operation;

import java.util.Calendar;
import java.util.TimeZone;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Message;

import com.zyw.customcalendar.event.EventIndex;

import de.greenrobot.event.EventBus;

public class CalendarDraw implements CalendarDrawInterface{

	//���»��Ƶ�����
	public float ex,ey;
	public RectF rdRect;
	public RectF cdRect;
	//�Ƿ��ػ����е�ͼ��
	public boolean isRDraw = true;
	private CalendarOperation calendarOperation;
	private static CalendarPaint calendarPaint;
	private final Calendar ccalendar = Calendar.getInstance();

	public CalendarDraw(){
		ccalendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		calendarOperation = new CalendarOperation();
		calendarPaint = new CalendarPaint();
	}

	public void initData(int width) {
		android.util.Log.i("zyw","initData 1");
		calendarPaint.setWeekGzWH(width);
	}

	public void Draw(Canvas canvas){
		//��ÿһ�λ���ǰ��������ջ���
		clear(canvas);
		calendarPaint.allGdHeight = 0;
		//drawYearBar(canvas);
		//drawWeeksBar(canvas);
		drawDaysBar(canvas);
		if(isRDraw == false){
			drawSelectBg(canvas, calendarPaint.returnDaysSBgPaint());
		}
	}
	
	public void clear(Canvas canvas)  {  
	    Paint paint = new Paint();  
	    paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));  
	    canvas.drawPaint(paint);  
	    paint.setXfermode(new PorterDuffXfermode(Mode.SRC));
	} 

	private void drawYearBar(Canvas canvas){
		//�߿������ɺ������ֵ
		Paint ybgpaint = calendarPaint.returnYearBgPaint();
		int canvasWidth = canvas.getWidth();

		RectF yearBgRectf = new RectF(0,0,canvasWidth,calendarPaint.yearGzHeight);
		canvas.drawRect(yearBgRectf, ybgpaint);
		drawYearText(canvas);
	}

	private void drawYearText(Canvas canvas){
		int i =0;
		Paint yfontpaint = calendarPaint.returnYearFontPaint();
		FontMetricsInt fontMetrics = yfontpaint.getFontMetricsInt();
		Rect targetRect = new Rect((int)(i*calendarPaint.weekGzWidth*2.7), 
				0, (int)((i+1)*calendarPaint.weekGzWidth*2.7), (int)(calendarPaint.yearGzHeight));
		int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
		canvas.drawText(calendarOperation.returnCurrentMonths(), targetRect.centerX(), baseline, yfontpaint);
	}

	private void drawWeeksBar(Canvas canvas){
		//�߿������ɺ������ֵ
		Paint wbgpaint = calendarPaint.returnWeeksBgPaint();
		int canvasWidth = canvas.getWidth();

		RectF WeeksBgRectf = new RectF(0,calendarPaint.yearGzHeight,canvasWidth,calendarPaint.weekGzHeight + calendarPaint.yearGzHeight);
		canvas.drawRect(WeeksBgRectf, wbgpaint);
		drawWeeksText(canvas);
	}

	private void drawWeeksText(Canvas canvas){
		Paint wfontpaint = calendarPaint.returnWeeksFontPaint();
		for(int i = 0; i < calendarOperation.weeks.length; i++){
			FontMetricsInt fontMetrics = wfontpaint.getFontMetricsInt();
			Rect targetRect = new Rect((int)(i*calendarPaint.weekGzWidth), 
					(int)calendarPaint.yearGzHeight, (int)((i+1)*calendarPaint.weekGzWidth), (int)(calendarPaint.weekGzHeight + calendarPaint.yearGzHeight));
			int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
			canvas.drawText(calendarOperation.weeks[i], targetRect.centerX(), baseline, wfontpaint);
		}
	}

	private void drawDaysBar(Canvas canvas){
		Paint dbgpaint = calendarPaint.returnDaysBgPaint();
		int canvasWidth = canvas.getWidth();

		int k = 0; //���ڼ�¼monthDays���±�
		float initHeight = 0;//calendarPaint.weekGzHeight + calendarPaint.yearGzHeight;
		for(int i = 0; i < 6; i++){
			if(k < 28 || calendarOperation.monthDays[k] != 0){
				Rect daysBgRect = new Rect(0,(int)(initHeight+(i*calendarPaint.dayGzHeight)),
						(int)(canvasWidth),(int)((i + 1)*calendarPaint.dayGzHeight + initHeight));
				canvas.drawRect(daysBgRect, dbgpaint);
				calendarPaint.allGdHeight += calendarPaint.dayGzHeight;
				//�߿������ɺ������ֵ
				drawDaysText(canvas, i, k);
			}
			k=k+7;
		}
	}

	private void drawDaysText(Canvas canvas, int r, int k){
		float initHeight = 0;//calendarPaint.weekGzHeight + calendarPaint.yearGzHeight;		//���Ƶĳ�ʼ�߶�
		Paint dGlFontPaint = calendarPaint.returnDaysGLFontPaint();
		Paint dNlFontPaint = calendarPaint.returnDaysNLFontPaint();

		for(int i = k; i < k + calendarOperation.weeks.length; i++){
			if(calendarOperation.monthDays[i] != 0){
				FontMetricsInt fontMetrics = dGlFontPaint.getFontMetricsInt();
				FontMetricsInt fontNlMetrics = dNlFontPaint.getFontMetricsInt();
				Rect targetRect = new Rect((int)((i%7)*calendarPaint.weekGzWidth), 
						(int)(initHeight+(r*calendarPaint.dayGzHeight)), (int)(((i%7)+1)*calendarPaint.weekGzWidth), 
						(int)((r + 1)*calendarPaint.dayGzHeight + initHeight));
//				//���������ǲ��Ǳ�ѡ�е�����
//				if(calendarOperation.monthDays[i] == calendarOperation.selectDayOfMonth){
//					post(i);
//				}
				if(calendarOperation.monthDays[i] == calendarOperation.currentDayOfMonth && 
						ccalendar.get(Calendar.MONTH) == calendarOperation.returnSelectCalendar().get(Calendar.MONTH)){
					//���㳤��֮��xx���ڼ���ѡ�б������ο�ʱ����Ҫ��ȥxx�ĸ߶�
					float xx = (float) ((calendarPaint.weekGzWidth - calendarPaint.dayGzHeight) / 2.0);
					cdRect = new RectF(targetRect.left,targetRect.top - (xx),
							targetRect.right,targetRect.bottom + (xx));
					drawCurrentBg(canvas, calendarPaint.returnDaysCBgPaint());
				}
				int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2
						- (fontMetrics.bottom - fontMetrics.top)/2;
				canvas.drawText(String.valueOf(calendarOperation.monthDays[i]), targetRect.centerX(), baseline, dGlFontPaint);
				//����ũ������
				baseline = (targetRect.bottom + targetRect.top - fontNlMetrics.bottom - fontNlMetrics.top) / 2
						+ (fontNlMetrics.bottom - fontNlMetrics.top)/2;
				canvas.drawText(calendarOperation.nlDays[i], targetRect.centerX(), baseline, dNlFontPaint);
				//�����¼�
				String eventKey = calendarOperation.returnDays(calendarOperation.monthDays[i]);
				if(calendarOperation.eventDays.get(eventKey) != null){
					baseline = (int) (baseline + calendarPaint.weekGzWidth*0.5);
					float xx = (float) ((calendarPaint.weekGzWidth - calendarPaint.dayGzHeight) / 2.0);
					
					int x = (targetRect.left + targetRect.right) / 2;
					int y = (int) (targetRect.bottom + (xx));
					//����ƫ����   0.07��ϵ��
					int tx = (int) (calendarPaint.weekGzWidth*0.07);
					cdRect = new RectF(x - tx, y - tx*3,x + tx, y - tx);
					drawCurrentBg(canvas, calendarPaint.returnEventsBdPaint());
				}
			}
		}
	}

	//����ѡ�б���ɫ
	private void drawSelectBg(Canvas canvas, Paint paint){
		canvas.drawOval(rdRect, paint);
	}

	//���Ƶ�ǰ���ڵı���ɫ
	private void drawCurrentBg(Canvas canvas, Paint paint){
		canvas.drawOval(cdRect, paint);
	}

	//���»����������
	public int setRdRect(){
		float initHeight  = 0;//rcalendarPaint.weekGzHeight + calendarPaint.yearGzHeight;
		float sjx = ex;
		float sjy = ey - initHeight;
		if(sjy > 10 && sjy < calendarPaint.allGdHeight){
			int col = (int)(sjx / calendarPaint.weekGzWidth);			//����������
			int row = (int)(sjy / calendarPaint.dayGzHeight);			//����������
			int k = row * 7 + col;
			if(k < 0 || calendarOperation.monthDays[k] == 0)
				return -1;
			android.util.Log.i("zyw","setRdRect 1 row = " + row + " / col = " + col);
			calendarOperation.selectDayOfMonth = calendarOperation.monthDays[k];
			//���㳤��֮��xx���ڼ���ѡ�б������ο�ʱ����Ҫ��ȥxx�ĸ߶�
			float xx = (float) ((calendarPaint.weekGzWidth - calendarPaint.dayGzHeight) / 2.0);
			rdRect = new RectF((int)((col%7)*calendarPaint.weekGzWidth),(int)(initHeight+(row*calendarPaint.dayGzHeight) - xx),
					(int)(((col%7)+1)*calendarPaint.weekGzWidth),(int)((row + 1)*calendarPaint.dayGzHeight + initHeight + xx));
			return k;
		}
		return -1;
	}

	public int setRdRect(int selectDayofMonth){
		int k = -1;
		float initHeight = 0;
		for(int i = 0; i < calendarOperation.monthDays.length; i++){
			if(calendarOperation.monthDays[i] == selectDayofMonth){
				calendarOperation.selectDayOfMonth = selectDayofMonth;

				int row = i / 7;
				int col = i % 7;
				android.util.Log.i("zyw","setRdRect 2 row = " + row + " / col = " + col);
				float xx = (float) ((calendarPaint.weekGzWidth - calendarPaint.dayGzHeight) / 2.0);
				rdRect = new RectF((int)((col%7)*calendarPaint.weekGzWidth),(int)(initHeight+(row*calendarPaint.dayGzHeight) - xx),
						(int)(((col%7)+1)*calendarPaint.weekGzWidth),(int)((row + 1)*calendarPaint.dayGzHeight + initHeight + xx));
				k = i;
				break;
			}
		}
		return k;
	}

	public float setAllHeight(int width){
		int k = 0;
		float allHeight = 0;
		calendarPaint.allGdHeight = 0;
		calendarPaint.setWeekGzWH(width);
		for(int i = 0; i < 6; i++){
			if(k < 28 || calendarOperation.monthDays[k] != 0){
				calendarPaint.allGdHeight += calendarPaint.dayGzHeight;
			}
			k=k+7;
		}
		allHeight = allHeight + calendarPaint.allGdHeight + 0;
				//calendarPaint.weekGzHeight + calendarPaint.yearGzHeight;
		return allHeight;
	}

	//���⿪�ŵĽӿ�
	@Override
	public CalendarOperation getCalendarOperation() {
		// TODO Auto-generated method stub
		return calendarOperation;
	}

	@Override
	public CalendarPaint getCalendarPaint() {
		// TODO Auto-generated method stub
		return calendarPaint;
	}

	@Override
	public void setCalendarOperation(CalendarOperation calendarOperation) {
		// TODO Auto-generated method stub
		this.calendarOperation = calendarOperation;
	}

	@Override
	public void setCalendarPaint(CalendarPaint calendarPaint) {
		// TODO Auto-generated method stub
		this.calendarPaint = calendarPaint;
	}

	@Override
	public void setNextCalendar(Calendar calendar) {
		// TODO Auto-generated method stub
		calendarOperation.setNextCalendar(calendar);
	}

	@Override
	public void setPreviousCalendar(Calendar calendar) {
		// TODO Auto-generated method stub
		calendarOperation.setPreviousCalendar(calendar);
	}

	@Override
	public void refreshCalendar() {
		// TODO Auto-generated method stub
		calendarOperation.refreshCalendar();
	}

	public void post(int k){
		//发送添加事件
		int day = getCalendarOperation().monthDays[k];
		String startDate = getCalendarOperation().returnDays(day);
		Object value = getCalendarOperation().eventDays.get(startDate);
		android.util.Log.i("zyw","post day = " + day);

		int num = value != null ? (Integer)value : 0;
		Message message = new Message();
		message.what = EventIndex.MODEL_TAG_INDEX;
		message.obj = num > 0 ? startDate : null;
		message.arg1 = k;
		EventBus.getDefault().post(message);
	}

	public static float getDayHeight() {
		return calendarPaint.dayGzHeight;
	}
}
